package com.fs.ssm004.service.impl;

import com.fs.ssm004.Dao.CourseDao;
import com.fs.ssm004.pojo.Course;
import com.fs.ssm004.service.CourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    private CourseDao courseDao;

    @Autowired //自动装备
    public CourseServiceImpl(CourseDao courseDao) {
        this.courseDao = courseDao;
    }

    @Override
    public List<Course> getAll() {
        return (List<Course>) courseDao.queryAll();
    }

    @Override
    public PageInfo<Course> getUsersByPage(Integer currentPage, Integer pageSize) {
        //准备拦截mybatis后续的查询
        PageHelper.startPage(currentPage, pageSize);
        List<Course> courseList = courseDao.queryAll();
        System.out.println(courseList.size());
        //pagehelper内部计算总共有多少页
        PageInfo<Course> pageInfo = new PageInfo<>(courseList);
        return pageInfo;
    }

    @Override
    public Course getoneByCourseId(Integer c_id) {
        Course course = courseDao.queryById(c_id);
        System.out.println(course);
        return course;
    }

    @Override
    public boolean addOne(Course course) {
        int result = courseDao.insertById(course);
        return result > 0;
    }

    @Override
    public boolean removeOne(Course course) {
        int result = courseDao.deleteById(course);
        return result > 0;
    }

    @Override
    public boolean modifyOne(Course course) {
        int result = courseDao.updateById(course);
        return result > 0;
    }

    @Override
    public List<Course> getCourseThatNameLike(String classname) {
        return courseDao.queryByName(classname);
    }


}
